package com.wangjiacai.utils.utils;

import com.thoughtworks.xstream.XStream;

public class XStreamUtil {

    public static <T> T toBean(Class<T> clazz, String xml) {
        Object xmlObject = null;
        XStream xstream = new XStream();
        XStream.setupDefaultSecurity(xstream);
        xstream.allowTypes(new Class[]{clazz});
        xstream.processAnnotations(clazz);
        xstream.autodetectAnnotations(true);
        xmlObject = xstream.fromXML(xml);
        return (T)xmlObject;
    }

    public static String toXml(Object object) {
        XStream xstream = new XStream();
        xstream.processAnnotations(object.getClass());
        xstream.autodetectAnnotations(true);
        return xstream.toXML(object);
    }
}
